# ==============================================================================
# Run_All.R
# ==============================================================================
#
# Replication script for:
#
#   Atsusaka, Yuki; Holbrook, Jordan, 2026,
#   "Archive: A database of tabulated results from American ranked-choice
#    voting elections",
#   https://doi.org/10.7910/DVN/RBWU92, Harvard Dataverse, V1,
#   UNF:6:AMPd4kbOfRs4ku2sJUB9CA== [fileUNF]
#
# Paper website: www.archive-rcv.com
#
# PURPOSE:
#   This script reproduces all figures and tables presented in the paper.
#   It sequentially executes each individual script. All outputs are written
#   to the same directory as the scripts and input data.
#
# SCRIPTS EXECUTED:
#   1. Figure_4.R  - Coverage of Archive over time (PDF)
#   2. Figure_5.R  - Validation against official Minneapolis results (PDF)
#   3. Figure_6.R  - Comparison of winning margins (PDF)
#   4. Table_3.R   - Data validation summary statistics (text)
#   5. Table_4.R   - Data quality checks (text)
#
# USAGE:
#   Run from the replication_package directory:
#     Rscript Run_All.R
#
# REQUIREMENTS:
#   - R packages: tidyverse, ggpubr, readxl, kableExtra
#   - All input data files in the same directory as this script
#
# SESSION INFORMATION
# > sessionInfo()
# R version 4.4.1 (2024-06-14 ucrt)
# Platform: x86_64-w64-mingw32/x64
# Running under: Windows 11 x64 (build 26200)
#
# Matrix products: default
#
#
# locale:
#   [1] LC_COLLATE=English_United States.utf8
# [2] LC_CTYPE=English_United States.utf8
# [3] LC_MONETARY=English_United States.utf8
# [4] LC_NUMERIC=C
# [5] LC_TIME=English_United States.utf8
#
# time zone: America/Chicago
# tzcode source: internal
#
# attached base packages:
#   [1] stats     graphics  grDevices utils     datasets  methods
# [7] base
#
# loaded via a namespace (and not attached):
#   [1] compiler_4.4.1    tools_4.4.1       rstudioapi_0.16.0
#
# ==============================================================================

library(tidyverse)

# --- Set working directory to this script's location -------------------------
# Ensures relative paths (source(), read_csv(), ggsave()) resolve correctly
# regardless of where the script is sourced from.
if (sys.nframe() > 0 && !is.null(sys.frame(1)$ofile)) {
  setwd(dirname(normalizePath(sys.frame(1)$ofile)))
}

cat("=== Replication Package ===\n")
cat("Paper: Atsusaka & Holbrook (2026)\n")
cat("       Archive: A database of tabulated results from American\n")
cat("       ranked-choice voting elections\n")
cat("DOI:   https://doi.org/10.7910/DVN/RBWU92\n")
cat("Web:   www.archive-rcv.com\n")
cat("Output:", normalizePath("."), "\n\n")

# --- Figure 4 -----------------------------------------------------------------
# Coverage of Archive over time.
# Reads archive-election.csv, archive-candidate.csv, and archive-round.csv.
# Produces a multi-panel figure saved as PDF.
cat("[1/5] Running Figure_4.R ...\n")
source("Figure_4.R")
cat("      -> Figure_4.pdf saved\n\n")

# --- Figure 5 -----------------------------------------------------------------
# Validation against official Minneapolis election results.
# Reads archive-candidate.csv, archive-election.csv, and
# minneapolis_scraped_election_results.csv.
# Produces a comparison figure saved as PDF.
cat("[2/5] Running Figure_5.R ...\n")
source("Figure_5.R")
cat("      -> Figure_5.pdf saved\n\n")

# --- Figure 6 -----------------------------------------------------------------
# Comparison of winning margins across elections.
# Reads archive-election.csv and cvr_files_summary.csv.
# Produces a scatter/comparison figure saved as PDF.
cat("[3/5] Running Figure_6.R ...\n")
source("Figure_6.R")
cat("      -> Figure_6.pdf saved\n\n")

# --- Table 3 ------------------------------------------------------------------
# Data validation summary statistics.
# Reads archive-election.csv and prints validation results.
# Console output is captured to a text file.
cat("[4/5] Running Table_3.R ...\n")
sink("Table_3.txt")
source("Table_3.R")
sink()
cat("      -> Table_3.txt saved\n\n")

# --- Table 4 ------------------------------------------------------------------
# Data quality checks.
# Reads archive-election.csv and prints summary statistics.
# Console output is captured to a text file.
cat("[5/5] Running Table_4.R ...\n")
sink("Table_4.txt")
source("Table_4.R")
sink()
cat("      -> Table_4.txt saved\n\n")

# --- Summary ------------------------------------------------------------------
cat("=== Replication complete ===\n")
cat("All outputs written to", normalizePath("."), "\n")
cat("\n")
